﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace LJLCommon.Helper
{
    /// <summary>
    /// 与字典映射的类
    /// </summary>
    [Serializable]
    public class DictClass
    {
        public byte key { get; set; }
        public object value { get; set; }
    }

    /// <summary>
    /// 对字典数据进行序列化处理
    /// </summary>
    public class DictToPackageHelper
    {
        public static string Serializa(Dictionary<byte, object> parameters)
        {
            List<DictClass> dictClassList = new List<DictClass>();

            foreach (var item in parameters)
            {
                DictClass dictClass = new DictClass() { key = item.Key, value = item.Value };
                dictClassList.Add(dictClass);
            }

            //将List序列化
            string listStr = Helper.XMLHelper.Serialze<List<DictClass>>(dictClassList);

            return listStr;
        }


        public static Dictionary<byte,object> DeSerializa(string listStr)
        {
            //将数据反序列化
            List<DictClass> dictClassList = Helper.XMLHelper.Deserialze<List<DictClass>>(listStr);

            Dictionary<byte, object> parameters = new Dictionary<byte, object>();

            foreach (var item in dictClassList)
            {
                parameters.Add(item.key, item.value);
            }

            return parameters;
        }
    }
}
